﻿using QYmall.Core.Common.Emum;
using QYmall.Core.Common.Extensions;
using QYmall.Core.Common.Helper;
using QYmall.Core.DBFactory.Database;
using QYmall.Core.IServices;
using QYmall.Core.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace QYmall.Core.Services
{
    /// <summary>
    /// 扩展属性接口实现
    /// </summary>
    public class ShopFieldService : BaseService, IShopFieldService
    {
        public ShopFieldService(IDbContextFactory contentFactory) : base(contentFactory) { }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        public async Task<ShopField?> QueryAsync(Expression<Func<ShopField, bool>> funcWhere,
            WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库
            var result = await _context.Set<ShopField>()
                .Include(x => x.Options)
                .FirstOrDefaultAsync(funcWhere);
            return result;
        }

        /// <summary>
        /// 查询指定数量列表
        /// </summary>
        public async Task<IEnumerable<ShopField>> QueryListAsync(int top, Expression<Func<ShopField, bool>> funcWhere,
            string orderBy, WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库
            var result = _context.Set<ShopField>()
                .Include(x => x.Options)
                .Where(funcWhere).OrderByBatch(orderBy);//调用Linq扩展类进行排序
            if (top > 0) result = result.Take(top);//等于0显示所有数据
            return await result.ToListAsync();
        }

        /// <summary>
        /// 查询分页列表
        /// </summary>
        public async Task<PaginationList<ShopField>> QueryPageAsync(int pageSize, int pageIndex, Expression<Func<ShopField, bool>> funcWhere,
            string orderBy, WriteRoRead writeAndRead = WriteRoRead.Read)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库
            var result = _context.Set<ShopField>()
                .Include(x => x.Options)
                .Where(funcWhere).OrderByBatch(orderBy);//调用Linq扩展类排序
            return await PaginationList<ShopField>.CreateAsync(pageIndex, pageSize, result);
        }

        /// <summary>
        /// 根据条件删除一条记录
        /// </summary>
        public async Task<bool> DeleteAsync(Expression<Func<ShopField, bool>> funcWhere, WriteRoRead writeAndRead = WriteRoRead.Write)
        {
            _context = _contextFactory.CreateContext(writeAndRead);//连接数据库
            var list = await _context.Set<ShopField>()
                .Include(x => x.Options)
                .Where(funcWhere).ToListAsync();
            if (list == null)
            {
                return false;
            }
            foreach (var modelt in list)
            {
                _context.Set<ShopField>().Attach(modelt);
            }
            _context.Set<ShopField>().RemoveRange(list);
            return await this.SaveAsync();
        }
    }
}
